package xyz.syyrjx.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.syyrjx.entity.ResultInfo;
import xyz.syyrjx.entity.User;
import xyz.syyrjx.service.MysqlRemoteService;
import xyz.syyrjx.service.RedisRemoteService;
import xyz.syyrjx.utils.JWTUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 模拟用户登录
 * @author Mr.Wan
 * @date 2022/10/11 - 20:41
 */
@RestController
public class UserController {
    @Resource
    private MysqlRemoteService mysqlService;
    @Resource
    private RedisRemoteService redisService;

    @RequestMapping("/login")
    public ResultInfo<Map<String,Object>> login(HttpServletRequest request,int id){
        ResultInfo<User> login = mysqlService.login(id);
        User user = login.getData();
        if (login.isFlag()){
            String token = JWTUtil.createToken(user.getUid());
            redisService.setToken(token,user);
            Map map = new HashMap();
            map.put("token", token);
            map.put("user", user);
            return ResultInfo.success(map);
        }
        return ResultInfo.fail("登录失败");
    }
}
